package com.apache.seq.service;

import com.apache.api.api.ApiService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * description: 序列模块统一对外接口实现
 */
public class SeqServiceImpl implements ApiService {

    private Logger log = LoggerFactory.getLogger(SeqServiceImpl.class);

    private Map<String, PluginConnector> plugins = new HashMap<String, PluginConnector>();

    /**
     * methodKey :seqNextValue 获取下一个序列值
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see com.apache.api.api.ApiService#doService(com.apache.api.vo.ParamsVo)
     */
    public ResultEntity doService(ParamsVo vo) throws ServiceException {
        ResultEntity result = new ResultEntity();
        result.setResult("false");
        if (Validator.isEmpty(plugins.get(vo.getMethodKey()))) {
            result.setMessage("没有确要执行的方法");
        } else {
            try {
                return (ResultEntity) plugins.get(vo.getMethodKey()).execute(vo);
            } catch (Exception e) {
                log.error("异常",e);
                throw new ServiceException("执行方法[" + vo.getMethodKey() + "]", e);
            }
        }
        return result;
    }

    public void setPlugins(Map<String, PluginConnector> plugins) {
        this.plugins = plugins;
    }

}
